/*
 * Copyright (c) 2011, Janni Kovacs
 * All rights reserved.
 *
 * Redistribution and use of this software in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above
 *   copyright notice, this list of conditions and the
 *   following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above
 *   copyright notice, this list of conditions and the
 *   following disclaimer in the documentation and/or other
 *   materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package de.umass.lastfm2itunes.core;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class WeeklyChartLoader {

	private String user;
	private DocumentBuilder builder;

	public WeeklyChartLoader(String user) {
		try {
			this.user = URLEncoder.encode(user, "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			this.user = user;
			// why is this exception checked, it never happens anyway, duh.. 
		}
		try {
			this.builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// meh...
		}
	}

	public List<Period> getWeeklyChartList() throws SAXException, IOException {
		Document document = builder.parse("http://ws.audioscrobbler.com/2.0/user/"+user+"/weeklychartlist.xml");
		List<Period> list = new LinkedList<Period>();
		NodeList tags = document.getElementsByTagName("chart");
		for (int i = 0; i < tags.getLength(); i++) {
			Element e = (Element) tags.item(i);
			list.add(new Period(new Date(1000 * Long.parseLong(e.getAttribute("from"))), new Date(1000 * Long
					.parseLong(e.getAttribute("to")))));
		}
		return list;
	}
	
	public List<ITrack> getWeeklyChart(Period p) throws SAXException, IOException {
		long from = p.from.getTime() / 1000;
		long to = p.to.getTime() / 1000;
		Document document = builder.parse("http://ws.audioscrobbler.com/2.0/user/"+user+"/weeklytrackchart.xml?from="+from+"&to="+to);
		List<ITrack> list = new LinkedList<ITrack>();
		NodeList tags = document.getElementsByTagName("track");
		for (int i = 0; i < tags.getLength(); i++) {
			Element e = (Element) tags.item(i);
			list.add(new ITrack(e.getElementsByTagName("artist").item(0).getTextContent(),
					e.getElementsByTagName("name").item(0).getTextContent(),
					Integer.parseInt(e.getElementsByTagName("playcount").item(0).getTextContent())));
		}
		return list;
	}
}
